<template>
  <div>
    <el-table
      :data="cateList"
      border
      style="width: 100%"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="pid" label="分类编号" width="100">
      </el-table-column>
      <el-table-column prop="catename" label="分类名称" width="100">
      </el-table-column>
      <el-table-column prop="img" label="图片" width="180">
        <template slot-scope="item">
          <div>
            <img v-if="item.row.img" :src="item.row.img" class="img" alt="" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="item">
          <div>
            <el-tag effect="dark" type="success" v-if="item.row.status == 1">
              启用
            </el-tag>
            <el-tag effect="dark" type="warning" v-else> 禁用 </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="edit(item.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-delete"
              circle
              @click="del(item.row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapGetters, mapActions, mapState } from "vuex";
import { catedelete } from "../../require/api";
export default {
  data() {
    return {
      // istree: true,
    };
  },
  methods: {
    ...mapActions({ getCateAction: "cate/getCateAction" }),

    // 修改
    edit(e) {
      // console.log(id);
      this.$emit("edit", e);
    },

    // 删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // console.log(id);
          // 请求一条数据并删除
          catedelete({ id }).then((res) => {
            console.log(res);
            if (res.code == 200) {
              //
              this.$message.success(res.msg);
              // 更新页面
              this.$store.dispatch("cate/getCateAction");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  computed: {
    ...mapGetters({ cateList: "cate/getCate" }),
    ...mapState("cate", ["cateList"]),
  },
  mounted() {
    this.getCateAction();
  },
};
</script>
<style lang=less scoped >
.img {
  width: 80px;
  height: 80px;
}
</style>
